<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            display: flex;
            justify-content: center;
            height: 100vh;
            width: 100%;
        }

        .main {
            width: 70%;
            height: 100%;
            flex-direction: column;
            background-color: rgb(99, 213, 245);
            color: white;
            display: flex;

            align-items: center;
        }

        .title {
            display: block;
            width: 100%;
            font-size: 50px;
            font-weight: 800;
            text-align: center;
        }

        .form {
            width: 400px;
            height: 200px;
            border-radius: 16px;
            border: 1px solid;
            background-color: aliceblue;
            color: black;
            margin-top: 20px;
            font-size: 20px;
            justify-content: center;
        }

        .form p {
            text-align: center;
        }

        .form input,
        .form .gender select {
            cursor: pointer;
            border: 1px solid;
            border-radius: 16px;
            width: 140px;
        }

        .form .btn {
            margin-top: 20px;
            display: flex;
            justify-content: center;
        }

        .form .btn button {
            cursor: pointer;
            width: 100px;
            border-radius: 16px;
            border: 1px solid;

        }

        .tab {
            margin-top: 10px;
            width: 450px;
            border-radius: 12px;
            box-sizing: border-box;
            overflow: hidden;
        }

        .tab table {
            width: 100%;
            border-collapse: collapse;
        }

        .tab thead {
            background-color: rgb(99, 213, 245);
            color: white;
            /* 设置表头文字颜色 */
            font-weight: bold;
            /* 加粗文字 */
            text-align: center;
            /* 居中对齐 */
        }

        .tab thead th {
            border-radius: 16px;
            background-color: rgb(25, 194, 255);
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="title">学生系统</div>
        <div class="form">
            <div class="username">
                <p>学生姓名<input type="text" name="username" id="username"></p>
            </div>
            <div class="gender">
                <p>性别
                    <select name="gender" id="gender">
                        <option value="男">男</option>
                        <option value="女">女</option>
                        <option value="无">无</option>
                    </select>
                </p>
            </div>
            <div class="age">
                <p>年龄: <input type="text" name="age" id="age"></p>
            </div>
            <div class="btn">
                <span>
                    <button id="addbtn">添加</button>
                    <button id="sortbtn">排序</button>
                </span>
            </div>
        </div>
        <div class="tab">
            <table>
                <thead>
                    <tr>
                        <th>姓名</th>
                        <th>年龄</th>
                        <th>性别</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
</body>
<script>
    var arr = [];
    var add = document.getElementById('addbtn');
    var sort = document.getElementById('sortbtn');

    add.onclick = function () {
        var name = document.getElementById('username').value;
        var age = document.getElementById('age').value;
        var gender = document.getElementById('gender').value;
        var person = {
            name: name,
            age: age,
            gender: gender
        };
        arr.push(person);
        setTable(person)
    }
    function setTable(item) {
        var tbody = document.querySelector('tbody');
        var tr = document.createElement('tr');
        tbody.appendChild(tr);

        var td1 = document.createElement('td');
        td1.innerHTML = item.name;
        tr.append(td1);

        var td2 = document.createElement('td');
        td2.innerHTML = item.age;
        tr.append(td2)

        var td3 = document.createElement('td');
        td3.innerHTML = item.gender;
        tr.append(td3)

        var td3 = document.createElement('td');
        td3.innerHTML = "<button class='delete'>删除</button>";
        tr.append(td3)
        var delete_button = document.getElementsByClassName('delete');
        console.log(delete_button);
        for (var i = 0; i < delete_button.length; i++) {
            delete_button[i].onclick = function () {
                this.parentElement.parentElement.remove();
            }
        }
    }
    sort.onclick = function(){
        var res = arr.sort(function(a,b){
            return a.age-b.age;
        })
        var tbody = document.querySelector('tbody');
        tbody.innerHTML="";
        arr.forEach(function(item){
            setTable(item);
        })
    }




</script>

</html>